package config;

import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;

import java.awt.*;
import java.net.URI;

public class DemoConfig extends JFinalConfig {
    private static final int PORT = 8080;
    @Override
    public void afterJFinalStart() {
        super.afterJFinalStart();
        //启动项目后自动打开浏览器并访问项目
        if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {//判断是否支持Desktop.Action.BROWSE
            try {
                Desktop.getDesktop().browse(new URI("http://localhost:" + PORT + "/index.html"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void configConstant(Constants constants) {
        //在调用getPropertyToBoolean之前需要先调用loadPropertyFile
        loadPropertyFile("config.properties");
        //设置jfinal的开发模式
        constants.setDevMode(getPropertyToBoolean("devMode", true));
    }

    @Override
    public void configRoute(Routes routes) {
//        routes.add("/stationInfo", StationController.class);
        routes.scan("controller");
    }

    @Override
    public void configEngine(Engine engine) {

    }

    @Override
    public void configPlugin(Plugins plugins) {
        int initialSize = getPropertyToInt("initialSize");
        int minIdle = getPropertyToInt("minIdle");
        int maxActive = getPropertyToInt("maxActive");
        //获取jdbc连接池
        DruidPlugin druidPlugin = new DruidPlugin(getProperty("jdbcUrl"), getProperty("username").trim(), getProperty("password").trim());
        druidPlugin.set(initialSize, minIdle, maxActive);
        plugins.add(druidPlugin);
        //配置ActiveRecordPlugin插件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        //控制台显示sql语句
        arp.setShowSql(getPropertyToBoolean("showSql", true));
        plugins.add(arp);
        //添加映射，将数据库表映射到model
        arp.addMapping("mz_zk_d5k_voltage_bak", "Id", model.StationVoltageInfo.class);
    }


    @Override
    public void configInterceptor(Interceptors interceptors) {
        interceptors.addGlobalActionInterceptor(new interceptor.CorsInterceptor());
    }

    public void configHandler(Handlers handlers) {

    }
}
